var express = require('express');
//添加短信服务资源
const popCoreServer = require('@alicloud/pop-core');
var router = express.Router();

router.get('/getValidataCode', function(req, res, next) {

    //获取手机号
    let {phone} = req.query;
    console.log(phone);
    let code = parseInt(Math.random()*(9999-1000) + 1000);
    console.log(code);

    //--------------------------------------------------------
    //发送手机短信
    //阿里云的扣钱账号
    var popCore = new popCoreServer({
        accessKeyId: 'LTAI5tSjHMHdcNpwbmQKRkC4',
        accessKeySecret: 'tN4pxHQxKVo6dLJi6TPb2y32cLhdn6',
        // securityToken: '<your-sts-token>', // use STS Token
        endpoint: 'https://dysmsapi.aliyuncs.com',
        apiVersion: '2017-05-25'
    });

    //准备数据
    var params = {
        "SignName": "阿里云短信测试",
        "TemplateCode": "SMS_154950909",
        "PhoneNumbers": phone,
        "TemplateParam": "{\"code\":\""+code+"\"}"
    }
    //发送到手机的传输方式
    var requestOption = {
        method: 'POST',
        formatParams: false,
    };
    //发送验证码请求
    popCore.request('SendSms', params, requestOption).then((result) => {
        console.log(JSON.stringify(result));
        res.send({"code":code});
    }, (ex) => {
        console.log(ex);
        res.send({"code":"err"});
    })
});

module.exports = router;
